﻿Imports System.Reflection
Public Class Engine
    Private _Settings As AccountSettings
    Private _AccountName As String
    Public Event StatusUpdated(ByVal status As String)
    Public Sub New(ByVal account As String)
        Dim config As New Configbase(Of AccountSettings)(account)
        Me._Settings = config.ReadSettings
        Me._AccountName = account
    End Sub
    Private Function LoadPlugin() As IMailNotifyPlugin
        Dim mailNotify As Assembly = Assembly.GetExecutingAssembly
        Dim plugin As IMailNotifyPlugin = mailNotify.CreateInstance(Me._Settings.Plugin, True)
        Return plugin
    End Function
    Public ReadOnly Property NewMails() As Integer
        Get
            Dim result As Integer = 0
            Dim mailPlugin As IMailNotifyPlugin = Me.LoadPlugin()
            AddHandler mailPlugin.StatusChanged, AddressOf Me.StatusChanged
            'Currently passing nothing, may need pass application details.
            mailPlugin.Initialize(Nothing)
            mailPlugin.CheckMail(Me._AccountName)
            If mailPlugin.Exception Is Nothing Then
                result = mailPlugin.NewMailCount
            End If
            Return result
        End Get
    End Property
    Private Sub StatusChanged(ByVal Status As String)
        RaiseEvent StatusUpdated(Status)
    End Sub
End Class
